package com.hbt.myplus.controller;

import com.hbt.myplus.model.PetOwn;
import com.hbt.myplus.service.impl.PetOwnServiceImpl;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.hbt.myplus.service.IFeedbackService;
import com.hbt.myplus.service.impl.FeedbackServiceImpl;
import com.hbt.myplus.model.Feedback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.hbt.myplus.utils.ResponseWeb;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
    *  前端控制器
    * </p>
 *
 * @author astupidcoder
 * @since 2020-12-22
 * @version v1.0
 */
@RestController
@RequestMapping("feedback")


public class FeedbackController {

    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private FeedbackServiceImpl feedbackService;

    @Autowired
    private PetOwnServiceImpl petOwnService;

    /**
     * 查询分页数据
     */
    @GetMapping(value = "/s")
    public ResponseWeb<Page> findListByPage(@RequestParam(name = "pageNum", defaultValue = "1") int pageNum,@RequestParam(name = "pageSize", defaultValue = "20") int pageSize){
        return null;
    }

    /**
     * 查询所有数据
     */
    @GetMapping(value = "/all")
    public ResponseWeb all(){
        List<Feedback> list = feedbackService.list();
        List<PetOwn> petOwnList = petOwnService.list();
        if (list == null || list.size() == 0){
            list = new ArrayList<>(0);
        }else {
            for (Feedback item : list) {
                PetOwn petOwn = petOwnList.stream().filter(a -> a.getOpenid().equals(item.getPoid())).collect(Collectors.toList()).get(0);
                item.setPetOwn(petOwn);
            }
        }
        return new ResponseWeb(list);
    }


    /**
     * 根据id查询
     */
    @GetMapping(value = "/{id}")
    public ResponseWeb<Feedback> getById(@PathVariable("id") String id){
       return null;
    }

    /**
     * 新增
     */
    @PostMapping
    public ResponseWeb add(@RequestBody Feedback feedback, @RequestHeader("openid") String openid){
        feedback.setTime(new Date());
        feedback.setPoid(openid);
        feedbackService.save(feedback);
        return new ResponseWeb("反馈成功");
    }

    /**
     * 删除
     */
    @DeleteMapping
    public ResponseWeb<String> delete(@RequestParam("ids") List<String> ids){
        return null;
    }

    /**
     * 修改
     */
    @PutMapping
    public ResponseWeb<Feedback> update(@RequestBody Feedback feedback){
        return null;
     }

}
